Option Strict On
Option Explicit On 

Imports System.Windows.forms
Imports C1.Win.C1Input


Public Enum allowNull
    YES = 0
    NO = 1
End Enum

''' -----------------------------------------------------------------------------
''' Project	 : IPCommon
''' Class	 : Core.IPCommon.CValidateTextBox
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Validate textbox content before data committing
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	2/21/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CValidateTextBox
    Public Shared Function IsValidPosValue(ByVal i_txtCtrl As TextBox _
                , Optional ByVal i_displayDefaultMsg As Boolean = True) As Boolean
        Dim v_strText As String = i_txtCtrl.Text.Trim
        Dim v_textbox_is_valid As Boolean = CUtil.IsValidPosNumber(v_strText)

        If Not v_textbox_is_valid Then
            CErrorTextBoxHandler.markAsErrorTxtBox(i_txtCtrl)
            If i_displayDefaultMsg Then
                BaseMessages.MsgBox_Warning(20)
            End If
        End If

        Return v_textbox_is_valid
    End Function

    Public Shared Function IsValidNotNegValue(ByVal i_txtCtrl As TextBox _
                , ByVal i_AllowNull As allowNull _
                , Optional ByVal i_displayDefaultMsg As Boolean = True) As Boolean


        Dim v_textbox_is_valid As Boolean = True
        Dim v_strText As String = i_txtCtrl.Text.Trim
        If v_strText = "" Then
            'Kiem tra dieu kien rong
            Select Case i_AllowNull
                Case allowNull.NO
                    v_textbox_is_valid = False
                    If i_displayDefaultMsg Then
                        BaseMessages.MsgBox_Warning(1)
                    End If
            End Select
        Else 'Trong truong hop khac rong
            'Nếu không phải là số
            If Not CUtil.IsValidNumber(v_strText) Then
                v_textbox_is_valid = False
                'Là số âm
            ElseIf CUtil.IsValidNegNumber(v_strText) Then
                v_textbox_is_valid = False
            End If
            If i_displayDefaultMsg And Not v_textbox_is_valid Then
                BaseMessages.MsgBox_Warning(21)
            End If
        End If

        If Not v_textbox_is_valid Then
            CErrorTextBoxHandler.markAsErrorTxtBox(i_txtCtrl)
        End If
        Return v_textbox_is_valid
    End Function

    Public Shared Function IsValid(ByVal i_txtCtrl As TextBox _
                , ByVal i_DataType As DataType _
                , ByVal i_AllowNull As allowNull _
                , Optional ByVal i_displayDefaultMsg As Boolean = True) As Boolean


        Dim v_textbox_is_valid As Boolean
        Dim v_strText As String = i_txtCtrl.Text.Trim
        If v_strText = "" Then
            'Kiem tra dieu kien rong
            Select Case i_AllowNull
                Case allowNull.NO
                    If i_displayDefaultMsg Then
                        BaseMessages.MsgBox_Warning(1)
                    End If
                    v_textbox_is_valid = False
                Case allowNull.YES
                    v_textbox_is_valid = True
            End Select
        Else  'Trong truong hop khac rong 
            Select Case i_DataType
                Case DataType.NumberType
                    v_textbox_is_valid = CUtil.IsValidNumber(v_strText, False)
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        BaseMessages.MsgBox_Warning(12)
                    End If
                Case DataType.DateType
                    v_textbox_is_valid = CDateTime.isValidDateString(v_strText, CDateTime.GetDateFormatString())
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        BaseMessages.MsgBox_Warning(14)
                    End If
                Case DataType.StringType
                    v_textbox_is_valid = True
            End Select
        End If

        If Not v_textbox_is_valid Then
            CErrorTextBoxHandler.markAsErrorTxtBox(i_txtCtrl)
        End If
        Return v_textbox_is_valid
    End Function


    Public Shared Function isValidStatusBarMsg(ByVal i_txtCtrl As TextBox _
                                    , ByVal i_DataType As DataType _
                                    , ByVal i_AllowNull As allowNull _
                                    , ByVal i_msg_handler As IPStatusBarMessages _
                                    , Optional ByVal i_displayDefaultMsg As Boolean = True) As Boolean

        Dim v_textbox_is_valid As Boolean
        Dim v_strText As String = i_txtCtrl.Text.Trim
        If v_strText = "" Then
            'Kiem tra dieu kien rong
            Select Case i_AllowNull
                Case allowNull.NO
                    If i_displayDefaultMsg Then
                        i_msg_handler.display_message(EnumCommonMessage.FILED_MUST_BE_FILLED_IN)
                    End If
                    v_textbox_is_valid = False
                Case allowNull.YES
                    v_textbox_is_valid = True
            End Select
        Else  'Trong truong hop khac rong 
            Select Case i_DataType
                Case DataType.NumberType
                    v_textbox_is_valid = CUtil.IsValidNumber(v_strText, False)
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        i_msg_handler.display_message(EnumCommonMessage.NUMERIC_DATA_MUST_BE_IN_CORRECT_FORMAT)
                    End If
                Case DataType.DateType
                    v_textbox_is_valid = CDateTime.isValidDateString(v_strText, CDateTime.GetDateFormatString())
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        i_msg_handler.display_message(EnumCommonMessage.DATE_DATA_MUST_BE_IN_CORRECT_FORMAT)
                    End If
                Case DataType.StringType
                    v_textbox_is_valid = True
            End Select
        End If

        If Not v_textbox_is_valid Then
            CErrorTextBoxHandlerStatusBar.markAsErrorTxtBoxStatusBar(i_txtCtrl, i_msg_handler)
        End If
        Return v_textbox_is_valid
    End Function
    '' Hàm kiểm tra C1 input
    Public Shared Function isC1ValidStatusBarMsg(ByVal i_txtCtrl As C1TextBox _
                                    , ByVal i_DataType As DataType _
                                    , ByVal i_AllowNull As allowNull _
                                    , ByVal i_msg_handler As IPStatusBarMessages _
                                    , Optional ByVal i_displayDefaultMsg As Boolean = True) As Boolean

        Dim v_textbox_is_valid As Boolean
        Dim v_strText As String = i_txtCtrl.Text.Trim
        If v_strText = "" Then
            'Kiem tra dieu kien rong
            Select Case i_AllowNull
                Case allowNull.NO
                    If i_displayDefaultMsg Then
                        i_msg_handler.display_message(EnumCommonMessage.FILED_MUST_BE_FILLED_IN)
                    End If
                    v_textbox_is_valid = False
                Case allowNull.YES
                    v_textbox_is_valid = True
            End Select
        Else  'Trong truong hop khac rong 
            Select Case i_DataType
                Case DataType.NumberType
                    v_textbox_is_valid = True
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        i_msg_handler.display_message(EnumCommonMessage.NUMERIC_DATA_MUST_BE_IN_CORRECT_FORMAT)
                    End If
                Case DataType.DateType
                    v_textbox_is_valid = CDateTime.isValidDateString(v_strText, CDateTime.GetDateFormatString())
                    If i_displayDefaultMsg And Not v_textbox_is_valid Then
                        i_msg_handler.display_message(EnumCommonMessage.DATE_DATA_MUST_BE_IN_CORRECT_FORMAT)
                    End If
                Case DataType.StringType
                    v_textbox_is_valid = True
            End Select
        End If

        If Not v_textbox_is_valid Then
            CErrorTextBoxHandlerStatusBar.markAsErrorC1TxtBoxStatusBar(i_txtCtrl, i_msg_handler)
        End If
        Return v_textbox_is_valid
    End Function
End Class


